//
//  BLDateTimePickView.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/8.
//

#import "BLDateTimePickView.h"

typedef void(^DatePickBlock)(NSDictionary * _Nullable dateDic);
@interface DateTimeCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *baseView;
@property (nonatomic,assign) BOOL isValid;

@end

@implementation DateTimeCell
-(void)awakeFromNib{
    [super awakeFromNib];
    self.baseView.layer.cornerRadius =11.5;
    self.baseView.layer.masksToBounds = YES;
}

-(void)setIsValid:(BOOL)isValid{
    _isValid = isValid;
    self.baseView.backgroundColor = [UIColor chColorWithHexString:@"#D0D0D0"];
    self.contentLabel.textColor = [UIColor chColorWithHexString:@"#555555"];
}

@end


@interface BLDateTimePickView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,assign) BOOL defaultSelected ;
@property(nonatomic,strong) NSIndexPath * defaultIndex;
@property(nonatomic,copy)NSArray * dataArray;
@property(nonatomic,copy) DatePickBlock  dateBlock;
@end
@implementation BLDateTimePickView

+(void)showDateTimePickView:(void(^)(NSDictionary * dic))block{
    BLDateTimePickView * pickView = [[BLDateTimePickView alloc]initWithFrame:BL_WINDOW_FRAME];
    pickView.dateBlock = block;
    [BL_WINDOW addSubview:pickView];
}

-(void)close:(UITapGestureRecognizer*)tap{
    [UIView animateWithDuration:.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


-(NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = @[@"09:00-10:00",@"10:00-11:00",@"11:00-12:00",@"12:00-13:00",@"13:00-14:00",@"14:00-15:00",@"15:00-16:00",@"16:00-17:00",@"17:00-18:00",@"18:00-19:00",@"19:00-20:00",@"20:00-21:00"];
    }
    return _dataArray;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self= [super initWithFrame:frame]) {
        [self setUi];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUi];
         NSLog(@"initWithCoder");
    }
    return self;
}

#define CELLINDN @"DateTimeCell"
#define baseViewH 305
#define gapW 27.5
-(void)setUi{
    
    self.backgroundColor = BL_COLOR_ALPHA;
    
    UIView * baseView = [[UIView alloc]initWithFrame:CGRectMake(0, BL_WINDOW_HEIGHT-baseViewH, BL_WINDOW_WIDTH, baseViewH)];
    baseView.backgroundColor = [UIColor whiteColor];
    [self addSubview:baseView];

    
    
    
    UIButton * cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    cancleBtn.titleLabel.textColor = [UIColor whiteColor];
    [cancleBtn setBackgroundColor:[UIColor chColorWithHexString:@"#C3C3C3"]];
    cancleBtn.layer.cornerRadius = 8.75;
    cancleBtn.layer.masksToBounds = YES;
    [cancleBtn addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:cancleBtn];
    
    
    UILabel * titleLable = [[UILabel alloc]init];
    titleLable.text = @"选择预约时间";
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.font = [UIFont systemFontOfSize:12];
    titleLable.textColor = [UIColor chColorWithHexString:@"#555555"];
    [baseView addSubview:titleLable];
 
    
    
    UIButton * sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    sureBtn.titleLabel.textColor = [UIColor whiteColor];
    [sureBtn setBackgroundColor:[UIColor chColorWithHexString:@"#6C72F5"]];
    sureBtn.layer.cornerRadius = 8.75;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:sureBtn];
    
    cancleBtn.frame = CGRectMake(22, 18.5, 40, 20);
    titleLable.frame = CGRectMake(0, 18.5, 150, 20);
    titleLable.middleX = baseView.middleX;
    sureBtn.frame = CGRectMake(BL_WINDOW_WIDTH-62, 18.5, 40, 20);
    
    
    
    CGRect frame = CGRectMake(15, CGRectGetMaxY(titleLable.frame) + 15, baseView.width -30, baseView.height - 90);
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 26;
    layout.minimumInteritemSpacing = 25;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView * item = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:layout];
    item.backgroundColor = [UIColor whiteColor];
    item.showsVerticalScrollIndicator = NO;
    item.showsVerticalScrollIndicator = NO;
    item.delegate = self;
    item.dataSource = self;
    [item registerNib:[UINib nibWithNibName:CELLINDN bundle:nil] forCellWithReuseIdentifier:CELLINDN];
    [baseView addSubview:item];

    [item reloadData];
}



-(void)sureClick{
    if (_dateBlock) {
        NSString * dateString = [self.dataArray objectAtIndex:_defaultIndex.row];
        _dateBlock(@{@"dateTime":dateString});
    }
    [UIView animateWithDuration:.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//MARK:delegate & datasource

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((BL_WINDOW_WIDTH - gapW * 4)/3, 32);
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1.0;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

//#warning UICollectionViewDelegateFlowLayout
////整体  返回内边距的上左下右的距离  每个section 内部的内容 距离headerView 的
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 0, 15);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DateTimeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELLINDN forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CELLINDN owner:nil options:nil] lastObject];
    }
    cell.contentLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    
    if (cell.selected) {
        cell.baseView.backgroundColor = [UIColor chColorWithHexString:@"#6B71F5"];
        cell.contentLabel.textColor = [UIColor whiteColor];
    }else{
        cell.baseView.backgroundColor = [UIColor chColorWithHexString:@"#E2E3FF"];
        cell.contentLabel.textColor = [UIColor chColorWithHexString:@"#6B71F5"];
    }
    
    if (indexPath.row == 0) {
        cell.baseView.backgroundColor = [UIColor chColorWithHexString:@"#6B71F5"];
        cell.contentLabel.textColor = [UIColor whiteColor];
        self.defaultIndex = indexPath;
    }
    
    return cell;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
       if( [collectionView cellForItemAtIndexPath:indexPath].selected){
           [collectionView deselectItemAtIndexPath:indexPath animated:NO];
           [self collectionView:collectionView didDeselectItemAtIndexPath:indexPath];
            return false;
       }
   
    return true;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!_defaultSelected) {
        DateTimeCell *cell = (DateTimeCell *)[collectionView cellForItemAtIndexPath:self.defaultIndex];
        cell.baseView.backgroundColor = [UIColor chColorWithHexString:@"#E2E3FF"];
        cell.contentLabel.textColor = [UIColor chColorWithHexString:@"#6B71F5"];
          _defaultSelected = YES;
      }
    DateTimeCell *cell = (DateTimeCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.baseView.backgroundColor = [UIColor chColorWithHexString:@"#6B71F5"];
    cell.contentLabel.textColor = [UIColor whiteColor];
    
}


//取消选定
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
  
    DateTimeCell *cell = (DateTimeCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.baseView.backgroundColor = [UIColor chColorWithHexString:@"#E2E3FF"];
    cell.contentLabel.textColor = [UIColor chColorWithHexString:@"#6B71F5"];
}

@end
